package com.hui.servlet;

import com.hui.entity.WareHouse;
import com.hui.service.WareHouseService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Description
 * @date 2023/2/11 8:53
 */
@WebServlet(urlPatterns = {"/getWareHouseAll", "/addWareHouse", "/delWareHouse", "/toUpdateWareHouse", "/updateWareHouse"})
public class WareHouseServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uri = req.getRequestURI();
        if (uri.endsWith("getWareHouseAll")) {
            getWareHouseAll(req, resp);
        } else if (uri.endsWith("addWareHouse")) {
            addWareHouse(req, resp);
        } else if (uri.endsWith("delWareHouse")) {
            delWareHouse(req, resp);
        } else if (uri.endsWith("toUpdateWareHouse")) {
            toUpdateWareHouse(req, resp);
        } else if (uri.endsWith("updateWareHouse")) {
            updateWareHouse(req, resp);
        }
    }

    /**
     * 修改数据
     **/
    private void updateWareHouse(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        int id = Integer.parseInt(req.getParameter("warehouseId"));
        String warehouseName = req.getParameter("warehouseName");
        String warehouseAddr = req.getParameter("warehouseAddr");
        String warehouseDesc = req.getParameter("warehouseDesc");
        WareHouse wareHouse = new WareHouse(id, warehouseName, warehouseAddr, warehouseDesc, "1");
        WareHouseService service = new WareHouseService();
        service.updateWareHouseById(wareHouse);

        resp.sendRedirect("getWareHouseAll");

    }

    /**
     * 跳转到修改界面，并传入数据
     **/
    private void toUpdateWareHouse(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = Integer.parseInt(req.getParameter("id"));

        WareHouseService service = new WareHouseService();
        WareHouse wareHouse = service.findWareHouseById(id);
        req.setAttribute("wareHouse", wareHouse);
        req.getRequestDispatcher("warehouse_update.jsp").forward(req, resp);


    }

    /**
     * 删除仓库数据
     **/
    private void delWareHouse(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int id = Integer.parseInt(req.getParameter("id"));

        WareHouseService service = new WareHouseService();
        service.delWareHouseById(id);

    }

    /**
     * 添加仓库
     **/
    private void addWareHouse(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        String warehouseName = req.getParameter("warehouseName");
        String warehouseAddr = req.getParameter("warehouseAddr");
        String warehouseDesc = req.getParameter("warehouseDesc");

        WareHouse wareHouse = new WareHouse(null, warehouseName, warehouseAddr, warehouseDesc, "1");

        WareHouseService service = new WareHouseService();
        service.addWareHouse(wareHouse);

        resp.sendRedirect("getWareHouseAll");

    }

    /**
     * 查询全部仓库并显示在前端界面
     **/
    private void getWareHouseAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        WareHouseService service = new WareHouseService();
        List<WareHouse> list = service.getWareHouseAll();

        req.setAttribute("list", list);
        req.getRequestDispatcher("warehouse_list.jsp").forward(req, resp);
    }
}
